<?php
class ModelConfigCommon extends Model {
	public function addCommon($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "config_common` SET url = '" . $this->db->escape($data['url']) . "', token = '" . $this->db->escape($data['token']) .  "', appid = '" . $this->db->escape($data['appid']) ."', appsecret = '" . $this->db->escape($data['appsecret']) . "', common_name = '" . "', status = '" . $this->db->escape($data['status']) . $this->db->escape($data['common_name']) . "', date_added = NOW()");
	}
	
	public function editCommon($common_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "config_common` SET url = '" . $this->db->escape($data['url']) . "', token = '" . $this->db->escape($data['token']) . "', status = '" . $this->db->escape($data['status']) . "', appid = '" . $this->db->escape($data['appid']) ."', appsecret = '" . $this->db->escape($data['appsecret']) . "', common_name = '" . $this->db->escape($data['common_name']). "' WHERE common_id = '" . (int)$common_id . "'");
	}
	
	
	public function getTotalCommons() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "config_common`");
		return $query->row['total'];
	}
	
	public function getCommon($common_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "config_common` WHERE common_id = '" . (int)$common_id . "'");
		return $query->row;
	}
	
	public function deleteCommon($common_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "config_common` WHERE common_id = '" . (int)$common_id . "'");
	}
	
	public function getCommons($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_common`";
			
		$sort_data = array(
		'status',
			'date_added'
		);	
			
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}

	public function getAllCommons()
	{
		$sql = "SELECT * FROM `" . DB_PREFIX . "config_common` where status=1";
		$query = $this->db->query($sql);
		return $query->rows;
	}
	
}
?>